package com.p2p.base.redis;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.p2p.base.cache.Cache;


/**
 * Redis接口封装
 * @author <a href="mailto:liyang@yiqihao.com">李阳</a>
 * @version 1.0
 */
public class LoginRedisImpl implements Cache {

	private RedisTemplate<String,Object> loginRedisTemplate;
	
//	public void add(String key,Object value) {
//		ValueOperations<String,Object> valueops = loginRedisTemplate
//				.opsForValue();
//		valueops.set(key, value);
//	}
	
	public RedisTemplate<String, Object> getLoginRedisTemplate() {
		return loginRedisTemplate;
	}


	public void setLoginRedisTemplate(
			RedisTemplate<String, Object> loginRedisTemplate) {
		this.loginRedisTemplate = loginRedisTemplate;
	}


	public void add(String key, Object value, long timeout) {
		ValueOperations<String,Object> valueops = loginRedisTemplate
				.opsForValue();
		valueops.set(key, value,timeout,TimeUnit.SECONDS);
	}

	
	public Object getValue(String key) {
		ValueOperations<String, Object> valueops = loginRedisTemplate
				.opsForValue();
		Object value = valueops.get(key);
		return value;
	}

	public void delete(String key) {
		loginRedisTemplate.delete(key);
	}
	
	/**
	 * 批量删除
	 */
	public void delete(Collection<String> keys) {
		loginRedisTemplate.delete(keys);
	}

	public Boolean exists(String key) {
		return loginRedisTemplate.hasKey(key);
	}

	@Override
	public boolean setIfAbsent(String key, Object value, long timeout) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Map<String, Object> getMulti(List<String> keys) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object[] getMulti(String[] keys) {
		// TODO Auto-generated method stub
		ValueOperations<String, Object> valueops = loginRedisTemplate
				.opsForValue();
		List<String> keys_list = Arrays.asList(keys);
		List<Object> value = valueops.multiGet(keys_list);
		return value.toArray();
	}

}
